package org.yjhking.xhr;

import io.netty.util.CharsetUtil;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 异步消息
 *
 * @author YJH
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建生产者组
        DefaultMQProducer producer = new DefaultMQProducer("producer-hello");
        //2.设置NameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动producer实例
        producer.start();
        //5.发送消息
        producer.send(//创建消息对象
                new Message("asyn-topic", "sms", "我是消息".getBytes(CharsetUtil.UTF_8)),
                //添加发送回调
                new SendCallback() {
                    //发送成功结果处理
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(sendResult);
                    }
                    
                    //发送异常结果处理
                    @Override
                    public void onException(Throwable throwable) {
                        System.out.println("发送异常：" + throwable.getMessage());
                    }
                });
    }
}